package com.hyg.lib_music.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.TabPagerAdapter;
import com.hyg.lib_music.ui.fragment.BaseFragment;

import java.util.List;


public class MusicTabView extends LinearLayout implements View.OnClickListener {
    LinearLayout nav;
    TextView navRecommend;//推荐你
    TextView navFav;//我喜欢
    TextView navMusicBase;//音乐库
//    TextView navKnowledge;//音乐知识

    ViewPager viewPager;
    onMyTabChangeListener listener;
    private FragmentManager mFragmentManager;
    private List<BaseFragment> mFragmentList;
    private TabPagerAdapter mAdapter;

    public MusicTabView(Context context) {
        this(context, null, 0);
    }

    public MusicTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MusicTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.view_music_tab, this);
        nav = view.findViewById(R.id.nav);
        navRecommend = view.findViewById(R.id.navRecommend);
        navFav = view.findViewById(R.id.navFav);
        navMusicBase = view.findViewById(R.id.navMusicBase);
//        navKnowledge = view.findViewById(R.id.navKnowledge);
        //设置点击事件
        navRecommend.setOnClickListener((OnClickListener) this);
        navFav.setOnClickListener((OnClickListener) this);
        navMusicBase.setOnClickListener((OnClickListener) this);
//        navKnowledge.setOnClickListener((OnClickListener) this);

        viewPager = view.findViewById(R.id.tab_viewPager);
        //设置默认的选中项
        selectTab(MenuTab.RECOMMEND);
    }

    /**
     * 外部调用初始化，传入必要的参数
     *
     * @param fm
     */
    public void init(FragmentManager fm, List<BaseFragment> fragmentList, onMyTabChangeListener listener) {
        mFragmentManager = fm;
        mFragmentList = fragmentList;
        this.listener = listener;
        initViewPager();
    }

    /**
     * 初始化 ViewPager
     */
    private void initViewPager() {
        mAdapter = new TabPagerAdapter(mFragmentManager, mFragmentList);
        viewPager.setAdapter(mAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //将ViewPager与下面的tab关联
                switch (position) {
                    case 0:
                        selectTab(MenuTab.RECOMMEND);
                        break;
                    case 1:
                        selectTab(MenuTab.MUSICBASE);
                        break;
                    case 2:
//                        selectTab(MenuTab.KNOWLEDGE);
//                        break;
//                    case 3:
                        selectTab(MenuTab.FAV);
                        break;
                    default:
                        selectTab(MenuTab.RECOMMEND);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 点击事件集合
     */
    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.navRecommend) {
            selectTab(MenuTab.RECOMMEND);
            //使ViewPager跟随tab点击事件滑动
            viewPager.setCurrentItem(0);
        } else if (id == R.id.navMusicBase) {
            selectTab(MenuTab.MUSICBASE);
            viewPager.setCurrentItem(1);
//        } else if (id == R.id.navKnowledge) {
//            selectTab(MenuTab.KNOWLEDGE);
//            viewPager.setCurrentItem(2);
        } else if (id == R.id.navFav) {
            selectTab(MenuTab.FAV);
            viewPager.setCurrentItem(2);
        }
    }

    /**
     * 设置 Tab 的选中状态
     *
     * @param tab 要选中的标签
     */
    public void selectTab(MenuTab tab) {
        //先将所有tab取消选中，再单独设置要选中的tab
        unCheckedAll();

//        //因为音乐知识背景色不同
        nav.setBackgroundColor(getResources().getColor(R.color.transparent));
        navRecommend.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab));
        navFav.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab));
        navMusicBase.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab));
//        navKnowledge.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab));

        switch (tab) {
            case RECOMMEND:
                navRecommend.setActivated(true);
                if(listener!=null){
                   listener.onTabChanged(0);
                }
                break;
            case FAV:
                navFav.setActivated(true);
                if(listener!=null){
                    listener.onTabChanged(2);
                }
                break;
            case MUSICBASE:
                navMusicBase.setActivated(true);
                if(listener!=null){
                    listener.onTabChanged(1);
                }
                break;
//            case KNOWLEDGE:
//                navKnowledge.setActivated(true);
//                nav.setBackgroundColor(getResources().getColor(R.color.musicTabBg));
//                navRecommend.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab_white));
//                navFav.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab_white));
//                navMusicBase.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab_white));
//                navKnowledge.setBackground(getResources().getDrawable(R.drawable.selector_bg_color_tab_white));
//                break;
        }

    }


    //让所有tab都取消选中
    private void unCheckedAll() {
        navRecommend.setActivated(false);
        navFav.setActivated(false);
        navMusicBase.setActivated(false);
//        navKnowledge.setActivated(false);
    }


    /**
     * tab的枚举类型
     */
    public enum MenuTab {
        RECOMMEND,//推荐
        FAV,//我喜欢
        MUSICBASE,//音乐库
//        KNOWLEDGE //音乐知识
    }


    public interface onMyTabChangeListener {
        void onTabChanged(int position);
    }

}
